var myApp = angular.module('myApp', [
    'ngAnimate',
    'ngResource',
    'ngSanitize',
    'ngTouch',
    'oc.lazyLoad',
    'ngStorage',
    'ui.router',
    'ui.bootstrap',
    'ui.utils',
    'ui.jq',
    'ngDialog',
    'angular-loading-bar',
    'toaster',
    'chart.js',
    'nvd3',
    'imgUrl'
]);
myApp.config(function ($stateProvider, $urlRouterProvider, $httpProvider) {
    /*跨域请求设置*/
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    /**
     * 设置post传值非json格式而是键值对方式
     * The workhorse; converts an object to x-www-form-urlencoded serialization.
     * @param {Object} obj
     * @return {String}
     */
    var param = function (obj) {
        var query = '', name, value;
        for (name in obj) {
            value = obj[name];
            if (value instanceof Array) {
                query += encodeURIComponent(name) + '=' + encodeURIComponent(angular.toJson(value)) + '&';
            }
            else if (value instanceof Object) {
                query += encodeURIComponent(name) + '=' + encodeURIComponent(angular.toJson(value)) + '&';
            }
            else if (value !== undefined && value !== null)
                query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
        }
        return query.length ? query.substr(0, query.length - 1) : query;
    };

    $httpProvider.defaults.transformRequest = [function (data) {
        return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];
});
myApp.config(function (cfpLoadingBarProvider, $httpProvider) {
    cfpLoadingBarProvider.includeSpinner = true;
    $httpProvider.interceptors.push('interceptor');
});
myApp.factory('interceptor', function ($injector, $localStorage, cfpLoadingBar, $q, toaster) {
    return {
        'request': function (request) {
            cfpLoadingBar.start();
            return request;
        },
        'response': function (response) {
            cfpLoadingBar.complete();
            var curCode = response && response.data && response.data.code,
                $state = $injector.get('$state');
            if (curCode == 901 ||
                curCode == 902 ||
                curCode == 903 ||
                //curCode == 904 ||
                curCode == 907 ||
                curCode == 908 ||
                curCode == 909 ||
                curCode == 910 ||
                curCode == 911) {
                toaster.info('提示', '请重新登录');
                $state.go('access.signin');

                if (response && response.data) {
                    response.data.message = '';
                }
                return $q.when(response)
            }
            return response || $q.when(response);
        },
        'requestError': function (rejection) {
            cfpLoadingBar.complete();
            if (!/\.html$/.test(rejection.url)) {
                console.log("网络请求错误！")
            }
            return rejection;
        },
        'responseError': function (rejection) {
            cfpLoadingBar.complete();
            if (!/\.html$/.test(rejection.url)) {
                console.log("网络响应错误！")
            }
            return rejection;
        }
    };
});